package com.ys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: note
 * @description:
 * @author:
 * @create 2020-06-11 22:01
 **/
public interface ConsumeRecordService {
    List<ConsumeRecord> getAll();
    List<ConsumeRecord> getAllByType(String type);
    void save(ConsumeRecord record);
    List<ConsumeRecord> getAllBetweenTime(String startTime, String endTime);

    /**
     * 指定时间段内的消费总花费
     * @param startTime
     * @param endTime
     * @return
     */
    default Double getTotalCostBetweenTime(String startTime, String endTime) {
        Double rtn = new Double(0.0d);
        List<ConsumeRecord> records = getAllBetweenTime(startTime, endTime);
        for (ConsumeRecord each : records) {
            rtn += each.getPrice() * each.getNum();
        }
        return rtn;
    }

    /**
     * 指定时间段内每件物品的消费数额
     * @param startTime
     * @param endTime
     * @return
     */
    default Map<String, Double> getNameCostMapBetweenTime(String startTime, String endTime) {
        Map<String, Double> map = new HashMap<>();
        List<ConsumeRecord> records = getAllBetweenTime(startTime, endTime);
        records.forEach(record -> {
            Double cost = map.get(record.getName());
            Double realCostValue = (cost == null) ? 0.0d : cost;
            map.put(record.getName(), realCostValue + record.getPrice() * record.getNum());
        });
        return map;
    }

    /**
     * 指定时间段内每件物品的购买数量
     * @param startTime
     * @param endTime
     * @return
     */
    default Map<String, Integer> getNameNumMapBetweenTime(String startTime, String endTime) {
        Map<String, Integer> map = new HashMap<>();
        List<ConsumeRecord> records = getAllBetweenTime(startTime, endTime);
        records.forEach(record -> {
            Integer num = map.get(record.getName());
            Integer realNum = (num == null) ? 0 : num;
            map.put(record.getName(), realNum + record.getNum());
        });
        return map;
    }

    /**
     * 指定时间段内购买数量最多的商品的数量和金额
     * @param startTime
     * @param endTime
     * @return
     */
    default ConsumeNameDetail getMostCostDetail(String startTime, String endTime) {
        Map<String, Double> map = getNameCostMapBetweenTime(startTime, endTime);
        Map.Entry<String, Double> bestEntry = null;
        for (Map.Entry<String, Double> entry : map.entrySet()) {
            if (bestEntry == null || bestEntry.getValue() < entry.getValue()) {
                bestEntry = entry;
            }
        }

        Map<String, Integer> numMap = getNameNumMapBetweenTime(startTime, endTime);
        String name = bestEntry.getKey();
        Integer num = numMap.get(name);
        Double cost = bestEntry.getValue();

        return new ConsumeNameDetail(name, num, cost);
    }

    /**
     * 指定时间段内各个类别商品的数量和金额
     * @param startTime
     * @param endTime
     * @return
     */
    default Map<String, ConsumeTypeDetail> getTypeCostMapBetween(String startTime, String endTime) {
        Map<String, ConsumeTypeDetail> map = new HashMap<>();
        List<ConsumeRecord> records = getAllBetweenTime(startTime, endTime);
        records.forEach(record -> {
            ConsumeTypeDetail detail = map.get(record.getType());
            if (detail == null) {
                map.put(record.getType(), new ConsumeTypeDetail(record.getType(),
                        record.getNum(), record.getPrice() * record.getNum()));
            } else {
                detail.setNum(detail.getNum() + record.getNum());
                detail.setCost(detail.getCost() + record.getPrice() * record.getNum());
            }
        });
        return map;
    }
}
